package com.zysdev.opengldouyin1demo

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.RadioGroup
import com.zysdev.opengldouyin1demo.filter.CameraView
import com.zysdev.opengldouyin1demo.widget.RecordButton

class MainActivity : AppCompatActivity(), RecordButton.OnRecordListener,
    RadioGroup.OnCheckedChangeListener {

    private var cameraView: CameraView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        cameraView = findViewById(R.id.cameraView)
        val btnRecord: RecordButton = findViewById(R.id.btn_record)
        btnRecord.setOnRecordListener(this)

        //速度
        val rgSpeed = findViewById<RadioGroup>(R.id.rg_speed)
        rgSpeed.setOnCheckedChangeListener(this)
        checkPermission()
    }


    private fun checkPermission(): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            requestPermissions(
                arrayOf(
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA
                ), 1
            )
        }
        return false
    }


    override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
        when (checkedId) {
            R.id.btn_extra_slow -> cameraView?.setSpeed(CameraView.Speed.MODE_EXTRA_SLOW)
            R.id.btn_slow -> cameraView?.setSpeed(CameraView.Speed.MODE_SLOW)
            R.id.btn_normal -> cameraView?.setSpeed(CameraView.Speed.MODE_NORMAL)
            R.id.btn_fast -> cameraView?.setSpeed(CameraView.Speed.MODE_FAST)
            R.id.btn_extra_fast -> cameraView?.setSpeed(CameraView.Speed.MODE_EXTRA_FAST)
        }
    }

    override fun onRecordStart() {
        cameraView?.startRecord()
    }

    override fun onRecordStop() {
        Log.i("tuch", "onRecordStop: ----------------->")
        cameraView?.stopRecord()
    }

}